package com.ohuang.test_koin

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.ohuang.test_koin.data.User
import com.ohuang.test_koin.presenter.UserPresenter
import com.ohuang.test_koin.presenter.UserPresenter2
import com.ohuang.test_koin.repository.LoginRepository
import com.ohuang.test_koin.ui.theme.Test_koinTheme
import org.koin.android.ext.android.inject
import org.koin.android.scope.AndroidScopeComponent
import org.koin.androidx.compose.getViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel
import org.koin.compose.koinInject
import org.koin.core.scope.Scope
import org.koin.java.KoinJavaComponent

class MainActivity : ComponentActivity() {val TAG="MainActivity"
    private val presenter: UserPresenter by inject()
    private val presenter2: UserPresenter by inject()
    private val viewModel:MainViewModel by viewModel()
    private val viewModel2:MainViewModel2 by viewModels()
    private val loginRepository:LoginRepository by inject()



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel.addUser()
        setContent {
            Test_koinTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    Column() {
                        Greeting(presenter = presenter)
                        Text(text = "启动", modifier = Modifier.clickable {
                            startActivity(Intent(this@MainActivity,MainActivity::class.java))
                        })
                        Text(text = "login(123,123) ${loginRepository.login("123","123")}")
                        Text(text = "viewModel= ${viewModel}")
                        Text(text = "viewModel2= ${viewModel2}")
                    }

                }
            }
        }
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.d(TAG, "onConfigurationChanged: ")
    }


}

@Composable
fun Greeting( modifier: Modifier = Modifier, presenter: UserPresenter= koinInject(),viewModel: MainViewModel= androidx.lifecycle.viewmodel.compose.viewModel()) {
    var name by remember {
        mutableStateOf("aaa")
    }
    Text(
        text = presenter.sayHello(name),
        modifier = modifier.clickable {
            name="ddd"
        }
    )
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
    Test_koinTheme {
        Greeting()
    }
}